import BlackBoard.InputSim;
import pgm;
from subprocess import call;
import time, random;

#Wait-N-Meet PPT Example
# sStatesFile = "./BlackBoard/StateSpace";
# sTransFile = "./BlackBoard/transitionM_ByAct"; #"./BlackBoard/transitionM";
# sPriorsFile = "./BlackBoard/priors_ByAct"; #"./BlackBoard/priors";
#RunChase Example
# sStatesFile = "./BlackBoard/StateSpace2";
# sTransFile = "./BlackBoard/transitionM2";
# sPriorsFile = "./BlackBoard/priors2";
#UofA Video Example
sStatesFile = "./BlackBoard/StateSpace3";
sTransFile = "./BlackBoard/transitionM3";
sPriorsFile = "./BlackBoard/priors3";
#3-Persons Example
# sStatesFile = "./BlackBoard/StateSpace4_3P";
# sTransFile = "./BlackBoard/transitionM4_3P";
# sPriorsFile = "./BlackBoard/priors4_3P";
#Multi-Persons Example
# sStatesFile = "./BlackBoard/StateSpace4_MultiP";
# sTransFile = "./BlackBoard/transitionM4_MultiP";
# sPriorsFile = "./BlackBoard/priors4_MultiP";


inputSim = BlackBoard.InputSim(pgm, sStatesFile, sTransFile, sPriorsFile, True);
blackB = BlackBoard.BlackBoard(inputSim);

#UofA Video Example
blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler/All.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/AllActs.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue1/P99_W.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue1/P99_L.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue1/xP99.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue1/yP99.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue1/zP99.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/AllActs.xml");
# aFiles = ["P103_L", "P103_W", "P103_S", "aP103", "bP103", "cP103", "dP103", "eP103", "fP103", "gP103", "hP103"];
# # aFiles = ["C", "B", "A"];
# random.shuffle(aFiles);
# for sFile in aFiles:
#     print sFile;
#     blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/"+sFile+".xml");
# #     blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler/"+sFile+".xml");
# # blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler/All.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outstandstill/standstill000.xml");

# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/zP103.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/P103_W.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/xP103.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/P103_L.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/yP103.xml");
# # nInitT = time.time();
# # for i in range(18):
# #     print "Reading file: standstill" + str(i).zfill(3) + ".xml";
# #     blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outstandstill/standstill" + str(i).zfill(3) + ".xml");
# # for i in range(41):
# #     print "Reading file: loiter" + str(i).zfill(3) + ".xml";
# #     blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outloiter/loiter" + str(i).zfill(3) + ".xml");
# # for i in range(181):
# #     print "Reading file: walk" + str(i).zfill(3) + ".xml";
# #     blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outwalk/walk" + str(i).zfill(3) + ".xml");
# # blackB.graph.setup();
# # print "Graph Creation Time:", time.time()-nInitT, "seconds ("+ str((time.time()-nInitT)/60.0) +" minutes).";
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outstandstill/standstill000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outstandstill/standstill001.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outwalk/walk000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outwalk/walk001.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Testing_PoL/PoL_Test.xml");

# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Testing_PoL/loiter.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Testing_PoL/standstill.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Testing_PoL/walk.xml");

# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outwalk/walk180.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outstandstill/standstill016.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/outwalk/walk181.xml");

#Testing PPT bigger Example
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmTest_Small.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmAll.xml");
# # blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmTest_Small9.xml");
# # blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/All.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/CHMM/All.xml");
# blackB.removeAct("2");
# blackB.removeAct("1");
# blackB.removeAct("3");
# blackB.removeAct("4");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/loiterB4.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/loiterA1.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/loiterA2.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/loiterA3.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/ChaseAB3.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/ChaseAB1.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/debugging/Issue2/Simpler2_PPT_Example/ChaseAB2.xml");

#Testing 3-Persons Example
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmTest_Small7_3P.xml");

#Testing Multi-Persons Example 
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/ActsHmmTest_Small1_MultiP.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/Test1_MultiP_5.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/Test1_MultiP_1.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/Test1_MultiP_4.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/Test1_MultiP_3.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/Test1_MultiP_2.xml");
# # blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Tests_MultiP/ActsHmmTest_Small5_MultiP.xml");


# aActSeq1 = ["300.15", "50.35", "50.60", "300.85", "300.100"];
# aActSeq2 = ["300.15", "300.35", "300.60", "300.85", "300.100"];
# aActSeq3 = ["300.15", "300.35", "800.60", "800.85", "300.100"];
# aActSeq4 = ["300.15", "50.35", "800.60", "300.85", "300.100"];
# aActSeqs = [aActSeq1, aActSeq2, aActSeq3, aActSeq4];
# print "getProbSeq:", blackB.getProbSeq(aActSeq1);
# print blackB.getMaxProbSeq(aActSeqs);
# print "StateProb:", blackB.getStateProb("60", [0,1,1,0,0]);

#print blackB.getMaxStProb("60");
# print blackB.getMaxProbSeq();
# # aActSeq = ["4.248", "4.398"];
# # aActSeq = ["4.248", "Null_person1.398"];
# # aActSeq = ["2.248", "Null_person1.398"];
# # aActSeq = ["Null_person1.248", "Null_person1.398"];
# # print "getProbSeq:", blackB.getProbSeq(aActSeq, bSmooth=False);
# blackB.getHmms(bSmooth=True);

#print "\nRemoving Nodes:";
#blackB.removeAct("00");
#blackB.removeAct("300");
#blackB.removeAct("300");
# blackB.removeAct("800");
# blackB.removeAct("300");
# blackB.removeAct("64");
# blackB.removeAct("58");

# blackB.makeUsrSegment(65);
# blackB.removeUsrSegment(65);


# #Print Posterior Probs for States at "t"
# aTimes = [15, 35, 60, 85, 100];
# print "\n---";
# for nT in aTimes:
#     [aNodes, aPosts, sT] = blackB.getStatePosts(nT, nT);
#     print "\n--State Posts of time:", sT;
#     for i,node in enumerate(aNodes):
#         print node.name+":", aPosts[i];

# # [aNodes, aPosts, sT] = blackB.getStatePosts(90, 100); #Run:34,34 Chase:80,80
# # print "\n--State Posts of time:", sT;
# # for i,node in enumerate(aNodes):
# #     print node.name+":", aPosts[i];
    
# # aActSeq1 = ["800.50", "800.90"];
# # aActSeq2 = ["50.50", "Null_A"];
# # nProbSeq1 = blackB.getProbSeq(aActSeq1);
# # nProbSeq2 = blackB.getProbSeq(aActSeq2);
# # print "---\ngetProbSeq1:", nProbSeq1;
# # print "getProbSeq2:", nProbSeq2;
# # 
# print blackB.getMaxProbSeq();
# # print "---\ngetProbSeq1:", nProbSeq1;


# # #Print Joint Probs for States at "t"
# # [aNodes, aPosts, sT] = blackB.getJointProb(15, 100);
# # print "\n---";
# # print "Joint of Nodes at time:", sT;
# # for node in aNodes:
# #     print node.name;
# # print "Joint States & Probs:"; 
# # for i,nP in enumerate(aPosts):
# #     print bin(i)[2:].zfill(len(aNodes))+", Prob:", nP;

# print "\nGraph Info:"
# blackB.printGraph();


# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/Case1/run000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/Case1/chase-first000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/Case1/chase-second000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/Case2/run000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/Case2/chase-first000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/Case2/chase-second000.xml");



# print "\nGraph Info:"
# blackB.printGraph();
nInitT = time.time();
# blackB.inferenceBB.getViterbi();
blackB.inferenceBB.getViterbi_CHMM();
# blackB.inferenceBB.getMaxProd_VE();
# blackB.inferenceBB.getMAP();
print "MAP method Time:", time.time()-nInitT, "seconds.";
# bSmooth = True;
# print "\n\n";
# blackB.getHmms(bSmooth=bSmooth);

# aActSeq1 = ["Null_person1.248", "Null_person1.398"]; #Null, Null
# aActSeq2 = ["2.248", "Null_person1.398"]; #Run, Null
# aActSeq3 = ["4.248", "Null_person1.398"]; #Chase, Null
# aActSeq4 = ["4.248", "4.398"]; #Chase, Chase
# print "\nPath Probabilities:";
# print "1. Null, Null:", blackB.getProbSeq(aActSeq1, bSmooth=bSmooth);
# print "2. Run, Null:", blackB.getProbSeq(aActSeq2, bSmooth=bSmooth);
# print "3. Chase, Null:", blackB.getProbSeq(aActSeq3, bSmooth=bSmooth);
# print "4. Chase, Chase:", blackB.getProbSeq(aActSeq4, bSmooth=bSmooth);
# # 
# aNodes = [];
# print "\n\nPosteriors:";
# blackB.setMExObs();
# # blackB.setEvObs(248);
# # blackB.setEvObs(398);
# for i,sK in enumerate(blackB.aActs_Time):
#     for j in range(len(blackB.aActs_Time[sK])):
#         aNodes.append(blackB.aActs_Time[sK][j].rootAct);
#         print "-State:", blackB.aActs_Time[sK][j].rootAct.name;
#     print i+1, "Priors:", blackB.graph.priorprob(aNodes);
#     print ""+ str(len(blackB.aActs_Time)-i) +":", blackB.graph.postprob(aNodes),"\n";
#     aNodes = [];

blackB.graph.setup();
blackB.graph.write2dot(fname='./BlackBoard/DynBB-graph.dot');
call(['C:/Program Files (x86)/Graphviz2.30/bin/dot.exe', '-Tpdf', './BlackBoard/DynBB-graph.dot', '-o', './BlackBoard/DynBB-graph.pdf']);
# call(['C:/Program Files (x86)/SumatraPDF/SumatraPDF.exe', './BlackBoard/DynBB-graph.pdf']);
# call(['dot', '-Tpdf', './BlackBoard/DynBB-graph.dot', '-o', './BlackBoard/DynBB-graph.pdf']);
# call(['evince', './BlackBoard/DynBB-graph.pdf']);